<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/2/19
 * Time: 16:10
 */

header('Content-type:text/html;charset=utf-8');

if (file_exists('./install.lock')) {
    echo '你已经安装过该系统，重新安装需要先删除public/install/install.lock 文件';
    die;
}

// 同意协议页面
if (@!isset($_GET['i']) || @$_GET['i'] == 'agreement') {
    require './agreement.html';
}

//检测环境
if (@$_GET['i'] == 'check') {
    require './check.html';
}

// 创建数据库页面
if (@$_GET['i'] == 'create') {
    require './create.html';
}

// 安装成功页面
if (@$_GET['i'] == 'success') {
    //判断是否为post
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $data = $_POST;
        // 连接数据库
        try {
            $link = new mysqli("{$data['DB_HOST']}:{$data['DB_PORT']}", $data['DB_USER'], $data['DB_PWD']);
        } catch (Exception $e) {
            die("<script>alert('数据库链接失败');history.go(-1)</script>");
        }
        // 设置字符集
        $link->query("SET NAMES 'utf8'");
        $link->server_info > 5.3 or die("<script>alert('请将您的mysql升级到5.0以上');history.go(-1)</script>");
        // 创建数据库并选中
        if (!$link->select_db($data['DB_NAME'])) {
            $create_sql = 'CREATE DATABASE IF NOT EXISTS ' . $data['DB_NAME'] . ' DEFAULT CHARACTER SET utf8;';
            $link->query($create_sql) or die('创建数据库失败');
            $link->select_db($data['DB_NAME']);
        }
        // 导入sql数据并创建表
        $bjyblog_str = file_get_contents('.sql');
        $sql_array = preg_split("/;[\r\n]+/", str_replace('bjy_', $data['DB_PREFIX'], $bjyblog_str));
        foreach ($sql_array as $k => $v) {
            if (!empty($v)) {
                $link->query($v);
            }
        }

    }
}

